/**
 * Copyright (c) 2015-2017, Henry Yang 杨勇 (gismail@foxmail.com).
 * <p>
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * <p>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.lambkit.module.upms.model;

import com.jfinal.kit.StrKit;
import com.lambkit.common.model.LambkitModel;
import com.lambkit.common.model.SortModel;
import com.lambkit.Lambkit;
import com.lambkit.db.sql.column.Column;
import com.lambkit.module.upms.UpmsConfig;
import com.lambkit.module.upms.UpmsManager;
import com.lambkit.module.upms.record.UpmsOrganization;
import com.lambkit.module.upms.row.IUpmsOrganization;
import com.lambkit.module.upms.server.service.UpmsOrganizationModelService;
import com.lambkit.module.upms.server.service.impl.UpmsOrganizationModelServiceImpl;
import com.lambkit.module.upms.sql.UpmsOrganizationSqlBuilder;

import java.util.ArrayList;
import java.util.List;

/**
 * @author yangyong
 * @website: www.lambkit.com
 * @email: gismail@foxmail.com
 * @date 2019-04-17
 * @version 1.0
 * @since 1.0
 */
public class UpmsOrganizationModel extends LambkitModel<UpmsOrganizationModel> implements IUpmsOrganization<UpmsOrganizationModel>, SortModel<UpmsOrganizationModel> {

	private static final long serialVersionUID = 1L;
	
	private List<UpmsUserModel> userList;

	public static UpmsOrganizationModelService service() {
		return Lambkit.get(UpmsOrganizationModelService.class, UpmsOrganizationModelServiceImpl.class);
	}

	public static UpmsOrganizationSqlBuilder sql() {
		return new UpmsOrganizationSqlBuilder();
	}

	public static UpmsOrganizationSqlBuilder sql(Column column) {
		UpmsOrganizationSqlBuilder that = new UpmsOrganizationSqlBuilder();
		that.add(column);
        return that;
    }

	public static UpmsOrganizationModel create(UpmsOrganization bean) {
		UpmsOrganizationModel model = new UpmsOrganizationModel();
		model._setOrPut(bean.getColumns());
		return model;
	}

	public UpmsOrganizationModel() {
		UpmsConfig config = UpmsManager.me().getConfig();
		String dbconfig = config.getDbconfig();
		if(StrKit.notBlank(dbconfig)) {
			this.use(dbconfig);
		}
	}

	public UpmsOrganization toPojo() {
		UpmsOrganization bean = new UpmsOrganization();
		bean.setColumns(this);
		return bean;
	}

	/***
	 * 查询区域及其所有下级节点
	 * @return
	 */
	public static List<UpmsOrganizationModel> findChildrenByPid(Long parentCode){
		String sql = "WITH RECURSIVE cte AS (SELECT n.* FROM "+ UpmsOrganizationModel.service().getTableName()+" AS n WHERE n.organization_id = ? " +
				"UNION ALL SELECT r.* FROM cte JOIN "+ UpmsOrganizationModel.service().getTableName()+" AS r ON r.pid = cte.organization_id ) SELECT * FROM cte";
		return UpmsOrganizationModel.service().dao().find(sql, parentCode);
	}
	
	public void fillUser() {
		userList = UpmsUserModel.service().findUserByOrgId(getOrganizationId());
	}

	private int layerNumber;
	private UpmsOrganizationModel parent;
	private List<UpmsOrganizationModel> childs;

	@Override
	public boolean isTop() {
		// TODO Auto-generated method stub
		return getPid() != null && getPid() == 0;
	}

	@Override
	public Long getModelId() {
		// TODO Auto-generated method stub
		return getOrganizationId();
	}

	@Override
	public Long getParentId() {
		// TODO Auto-generated method stub
		return getPid();
	}

	@Override
	public void setParent(UpmsOrganizationModel parent) {
		this.parent = parent;
	}

	@Override
	public UpmsOrganizationModel getParent() {
		return parent;
	}

	@Override
	public void setChilds(List<UpmsOrganizationModel> childs) {
		this.childs = childs;
	}

	@Override
	public void addChild(UpmsOrganizationModel child) {
		if (childs == null) {
			childs = new ArrayList<>();
		}
		childs.add(child);
	}

	@Override
	public List<UpmsOrganizationModel> getChilds() {
		return childs;
	}

	public boolean hasChild() {
		return childs != null && !childs.isEmpty();
	}

	@Override
	public void setLayerNumber(int layerNumber) {
		this.layerNumber = layerNumber;
	}

	@Override
	public int getLayerNumber() {
		return layerNumber;
	}

	public String getLayerString() {
		if (layerNumber == 0) {
			return "";
		}

		StringBuilder sb = new StringBuilder();
		for (int i = 0; i < layerNumber; i++) {
			if (i == 0) {
				sb.append("|—");
			} else {
				sb.append("—");
			}
		}
		return sb.toString();
	}

	public boolean isMyChild(long id) {
		if (childs == null || childs.isEmpty()) {
			return false;
		}

		return isMyChild(childs, id);
	}

	private boolean isMyChild(List<UpmsOrganizationModel> categories, long id) {
		for (UpmsOrganizationModel category : categories) {
			if (category.getModelId() == id) {
				return true;
			}

			if (category.getChilds() != null) {
				boolean isChild = isMyChild(category.getChilds(), id);
				if (isChild) {
					return true;
				}
			}
		}
		return false;
	}

	public List<UpmsUserModel> getUserList() {
		return userList;
	}

	public void setUserList(List<UpmsUserModel> userList) {
		this.userList = userList;
	}

}
